/**
 * 
 */
package com.base.myproject.client.tools;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.UIObject;

/**
 * 工具，常用的函数
 * @author zgw
 *2009-8-15 上午11:55:34
 * javazgw@gmail.com
 */
public class Tools {
/**
 * support int only
 * @param str
 * @return
 */
	public static boolean isDigit(String str)
	{
		if(str.equals(""))
			return false;
		char[] ch = str.toCharArray();
		for(char c:ch)
		{
		
			
			
			if(!java.lang.Character.isDigit(c))
			{
				
				return false;
			}
		}
		return true;
	}
	
	  /**
     * support Numeric format:<br>
     * "33" "+33" "033.30" "-.33" ".33" " 33." " 000.000 "
     * @param str String
     * @return boolean
     */
   public static boolean isNumeric(String str) {
       int begin = 0;
       boolean once = true;
       if (str == null || str.trim().equals("")) {
           return false;
       }
       str = str.trim();
       if (str.startsWith("+") || str.startsWith("-")) {
           if (str.length() == 1) {
               // "+" "-"
               return false;
           }
           begin = 1;
       }
       for (int i = begin; i < str.length(); i++) {
           if (!Character.isDigit(str.charAt(i))) {
               if (str.charAt(i) == '.' && once) {
                   // '.' can only once
                   once = false;
               }
               else {
                   return false;
               }
           }
       }
       if (str.length() == (begin + 1) && !once) {
           // "." "+." "-."
           return false;
       }
       return true;
   }

   /**
     * support Integer format:<br>
     * "33" "003300" "+33" " -0000 "
     * @param str String
     * @return boolean
     */
   public static boolean isInteger(String str) {
       int begin = 0;
       if (str == null || str.trim().equals("")) {
           return false;
       }
       str = str.trim();
       if (str.startsWith("+") || str.startsWith("-")) {
           if (str.length() == 1) {
               // "+" "-"
               return false;
           }
           begin = 1;
       }
       for (int i = begin; i < str.length(); i++) {
           if (!Character.isDigit(str.charAt(i))) {
               return false;
           }
       }
       return true;
   }


   /**
     * use Exception
     * support Numeric format:<br>
     * "33" "+33" "033.30" "-.33" ".33" " 33." " 000.000 "
     * @param str String
     * @return boolean
     */
   public static boolean isNumericEx(String str) {
       try {
           Double.parseDouble(str);
           return true;
       }
       catch (NumberFormatException ex) {
           return false;
       }
   }

   /**
     * use Exception
     * support less than 11 digits(<11)<br>
     * support Integer format:<br>
     * "33" "003300" "+33" " -0000 " "+ 000"
     * @param str String
     * @return boolean
     */
   public static boolean isIntegerEx(String str) {
       str = str.trim();
       try {
           Integer.parseInt(str);
           return true;
       }
       catch (NumberFormatException ex) {
           if (str.startsWith("+")) {
               return isIntegerEx(str.substring(1));
           }
           return false;
       }
   }
   
   public static DateTimeFormat getDefaultDTF()
   {
   DateTimeFormat activeFormat = DateTimeFormat
	.getFormat("yyyy-M-d H:mm:ss");
   return activeFormat;
   }
   
/**
 * 设置自动根据浏览器的窗体放大与缩小调整界面的比例
 * 只接受BoxComponent类与其子类。
 * Tools.setResize(grid.getBg(),0.5,0.95);
 * 是总体界面宽度的50%，总体界面高度的95%
 * ff,ie,chrome测试通过。
 * 
 * @param bc
 * @param width_per
 * @param height_per
 */
   public static void setResize(final UIObject bc,final double width_per,final double height_per)
   {
	   int width = Window.getClientWidth()-18;
		 int height = Window.getClientHeight()-115;
		 ResizeHandler resizehandler = new  ResizeHandler(){
			public void onResize(ResizeEvent event)
			 {
				
				int width = Window.getClientWidth()-18;
				int height = Window.getClientHeight()-115;

					bc.setSize(((int)(width * width_per))+"px",((int)(height * height_per))+"px");
			
				 GWT.log("resize1="+(int)(width * width_per)+""+(width * width_per));
				
			 }
		};
		//panel.setScrollMode(Scroll.AUTO);
		//----------
		
		Window.addResizeHandler(resizehandler);
		//bc.setSize((int)(width * width_per),(int)(height * height_per));
		bc.setSize(((int)(width * width_per))+"px",((int)(height * height_per))+"px");
   }
}
